import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Sun extends World
{
    private Lifes l;                                // Counter of the lifes availables.
    private Man man;
    private int punt;                               //First score.
    private Lifes score;                            //Message of score.
    private int puntaje;                            //Score.
    private int energia;
    private Bird b1 = new Bird(2);                  //There are two birds in this scenario.
    private Bird b2 = new Bird(2);
    private Home home = new Home(2);
    private Grass grass1 = new Grass(2);            //Scenario's grass.
    private Grass grass2 = new Grass(2);        
    private Apple apple2 = new Apple(2);            //Accesories.
    private Apple apple4 = new Apple(2);
    private Apple apple5 = new Apple(2);
    private Water water1 = new Water(2);
    private Water water2 = new Water(2);
    private Poison poison2 = new Poison(2);
    private Poison poison3 = new Poison(2);
    private Poison poison5 = new Poison(2);
    private Poison poison6 = new Poison(2);
    private JumpShoes shoes = new JumpShoes(2);
    private Mountain mountain = new Mountain(2);
    private Tree tree1 = new Tree("Arb_03.gif", 2);
    private Tree tree2 = new Tree("Arb_07.gif", 2);
    private Tree tree3 = new Tree("Arb_09.gif", 2);
    private Tree tree4 = new Tree("Arb_06.gif", 2);
    private Tree tree5 = new Tree("Arb_08.gif", 2);

    /**
     * Sun Constructor.
     */
    public Sun( int distance)
    {    
        super(600, 500, 1, false);          //New world.
        punt = 0;
        puntaje = 0;
        man = new Man(2);
        l = new Lifes( 3, "Lifes = ");      //Messages.
        score=new Lifes( 0, "Score = ");
        score.incrementa(distance);
        prepare();
    }

    public void act(){
        if( l.getNumLifes() == 0 || water1.lose() == true || water2.lose() == true)
        {                              //It shows on the screen "GAME OVER" when the player lost.
            Message lose = new Message( "  GAME OVER  ");
            addObject( lose, 250, 150);
            Greenfoot.delay( 50);
            Greenfoot.setWorld(new Menu( 0) );
        }
        checkFlagP( poison2);
        checkFlagP( poison3);
        checkFlagP( poison5);
        checkFlagP( poison6);
        checkFlagA( apple2);
        checkFlagA( apple4);
        checkFlagA( apple5);
        checkFlagJS( shoes);
        score.incrementa( puntaje() - punt);
        punt=puntaje();
    }

    /**
     * Prepare the world for the start of the program. 
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(grass1, 300, 450);
        addObject(grass2, getWidth(), 450);
        addObject( tree1, 120, 385);
        addObject( tree2, 240, 370);
        addObject( tree3, 360, 370);
        addObject( tree4, 480, 335);
        addObject( tree5, 600, 310);
        addObject( water1, 200, 410);
        addObject( water2, 400, 410);
        addObject( home, 3200, 110);
        addObject(apple2, 300, 400);
        addObject(apple4, 458, 400);
        addObject(apple5, 560, 400);
        addObject( poison2, 270, 400);
        addObject( poison3, 330, 400);
        addObject( poison5, 530, 400);
        addObject( poison6, 630, 400);
        addObject( score, 50, 30);
        addObject( shoes, 500, 400);
        addObject( mountain, 3100, 315);
        addObject( b1, 540, 50);
        addObject( b2, 140, 50);
        addObject( l, 550, 470);
        addObject( man, 160, 400);
        man.setLocation( 50,374); 
    }

    public Man getMan()
    {
        return man;
    }

    public Bird getBird1()
    {
        return b1;
    }

    public Bird getBird2()
    {
        return b2;
    }

    public Lifes getLifes()
    {
        return l;
    }

    public int level()
    {
        return 1;
    }

    public void checkFlagP( Poison pX)
    {
        if( man.toxic( pX) == true)
        {
            energia = energia - 50;
            removeObject( pX);}
    } 

    public void checkFlagA( Apple aX)
    {
        if( man.food( aX) == true){
            energia = energia + 50;
            removeObject( aX);}
    }

    public void checkFlagJS( JumpShoes jsX)
    {
        if( man.wear( jsX) == true)
            removeObject( jsX);
    }

    public int puntaje()
    {
        if( b1.punt() > puntaje)
            puntaje = b1.punt();
        return puntaje;
    }
}